﻿Imports Clo = Enhancements.Core.Logging
Imports Ere = Enhancements.Basic.RegularExpressions
Imports Exe = Enhancements.Basic.Expressions
Imports Gen = System.Collections.Generic
Imports Glo = System.Globalization
Imports Iou = System.IO
Imports Ref = System.Reflection
Imports Reg = System.Text.RegularExpressions
Imports Sys = System
Imports Tex = System.Text
Imports Xsd = System.Xml.Schema
Imports W3c = System.Xml

Namespace Enhancements.Basic.Expressions.Functions
    ''' <summary>Represents a token extracted from an expression that has a function name.</summary>
    <DebuggerStepThrough()> _
    Public NotInheritable Class FunctionName
        Inherits Exe.Token

        Private Sub New(ByVal _expression As System.String, ByVal _expressionBeginIndex As Sys.Int32, ByVal _expressionEndIndex As Sys.Int32, ByVal _function As [Function])
            MyBase.New(_expression, _expressionBeginIndex, _expressionEndIndex)
            [Function] = _function
        End Sub

        Private __Function As [Function] = Nothing
        ''' <summary>The function represented by this function name.</summary>
        ''' <exception cref="IsNullException">_value is null.</exception>
        Public Property [Function]() As [Function]
            Get
                Return __Function
            End Get
            Private Set(ByVal _value As [Function])
                Assert.IsNullNot(_value, "Function._value", True)

                __Function = _value
            End Set
        End Property

        Private Shared Function __Parse(ByVal _expression As System.String, ByVal _expressionBeginIndex As Sys.Int32, ByVal _function As [Function]) As Sys.Object
            Dim _returnValue As Sys.Object = Nothing
            If _expression.IsNullNot("_expression", False) AndAlso _expression.IsEmptyNot("_expression", False) AndAlso _expressionBeginIndex.IsInRange("_expressionBeginIndex", 0, _expression.Length - 1, False) Then
                If ___Texts.StartsWith(_expression.Substring(_expressionBeginIndex), _function.Name) Then
                    _returnValue = New FunctionName(_expression, _expressionBeginIndex, _expressionBeginIndex + _function.Name.Length - 1, _function)
                End If
            End If
            If Assert.IsNull(_returnValue, "_returnValue", False) Then
                _returnValue = FunctionNameParseException.___Create(_expression, _expressionBeginIndex)
            End If

            Return _returnValue
        End Function

        ''' <exception cref="IsNullNotException"></exception>
        ''' <exception cref="FunctionNameParseException"></exception>
        Friend Shared Function ___Parse(ByVal _expression As System.String, ByVal _expressionBeginIndex As Sys.Int32, ByVal _function As [Function]) As FunctionName
            Dim _returnValue As Sys.Object = __Parse(_expression, _expressionBeginIndex, _function)
            If Assert.IsOfType(_returnValue, "_returnValue", GetType(FunctionNameParseException), False) Then
                FunctionNameParseException.___Throw(CType(_returnValue, FunctionNameParseException))
            End If

            Return CType(_returnValue, FunctionName)
        End Function

        Friend Shared Function ___TryParse(ByVal _expression As System.String, ByVal _expressionBeginIndex As Sys.Int32, ByVal _function As [Function], ByRef _result As FunctionName) As System.Boolean
            Dim _returnValue As System.Boolean = False
            Dim _dummy As Sys.Object = __Parse(_expression, _expressionBeginIndex, _function)
            If Assert.IsOfType(_dummy, "_dummy", GetType(FunctionNameParseException), False) Then
                _result = Nothing
                _returnValue = False
            Else
                _result = CType(_dummy, FunctionName)
                _returnValue = True
            End If

            Return _returnValue
        End Function

        ''' <summary>Determines the token's correct representation for use in an expression.</summary>
        ''' <returns> </returns>
        Public Overrides Function ToRepresentation() As System.String
            Dim _returnValue As System.String = [Function].Name

            Return _returnValue
        End Function

    End Class
End Namespace
